#   库的引用
import os.path
import datetime

import PyQt6.QtWidgets as QtWidgets
import PyQt6.QtGui as QtGui

#   日历视图
class Calend(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.basePath = os.path.dirname(os.path.abspath(__file__))
        self.initUI()
    
    def initUI(self):
        #   设置主题
        self.stylePath = os.path.join(self.basePath, "Qss/calend.qss")
        self.qssFile = open(self.stylePath, 'r')
        self.qssStyle = self.qssFile.read()
        self.setStyleSheet(self.qssStyle)

        #   主Layout
        self.vbox = QtWidgets.QVBoxLayout()
        self.vbox.setContentsMargins(0, 0, 0, 0)
        self.vbox.setSpacing(6)
        self.setLayout(self.vbox)

        #   日历区域
        self.calWidget = QtWidgets.QFrame()
        self.vbox.addWidget(self.calWidget)
        self.calLayout = QtWidgets.QVBoxLayout()
        self.calLayout.setContentsMargins(0, 0, 0, 0)
        self.calLayout.setSpacing(0)
        self.calWidget.setLayout(self.calLayout)

        self.widgetDate = QtWidgets.QFrame()
        self.widgetDate.setStyleSheet("QPushButton{border: 1px solid gray; background: #bacac6}"
                                      "QPushButton:hover{background: #88ada6}"
                                      "QPushButton:pressed{border: 1px solid black}")
        self.hboxDate = QtWidgets.QHBoxLayout()
        self.widgetDate.setLayout(self.hboxDate)
        self.hboxDate.setContentsMargins(0, 0, 0, 0)
        self.hboxDate.setSpacing(6)
        self.calLayout.addWidget(self.widgetDate)

        #   控制与信息区
        self.btnLastyear = QtWidgets.QPushButton()
        self.iconPath = os.path.join(self.basePath, "Icon/lleft.png")
        self.btnLastyear.setIcon(QtGui.QIcon(self.iconPath))
        self.hboxDate.addWidget(self.btnLastyear)
        self.btnLastyear.clicked.connect(self.toLastyear)

        self.btnLast = QtWidgets.QPushButton()
        self.iconPath = os.path.join(self.basePath, "Icon/left.png")
        self.btnLast.setIcon(QtGui.QIcon(self.iconPath))
        self.hboxDate.addWidget(self.btnLast)
        self.btnLast.clicked.connect(self.toLast)

        self.hboxDate.addStretch(1)

        self.labDate = QtWidgets.QLabel()
        self.hboxDate.addWidget(self.labDate)

        self.hboxDate.addStretch(1)

        self.btnNext = QtWidgets.QPushButton()
        self.iconPath = os.path.join(self.basePath, "Icon/right.png")
        self.btnNext.setIcon(QtGui.QIcon(self.iconPath))
        self.hboxDate.addWidget(self.btnNext)
        self.btnNext.clicked.connect(self.toNext)

        self.btnNextyear = QtWidgets.QPushButton()
        self.iconPath = os.path.join(self.basePath, "Icon/rright.png")
        self.btnNextyear.setIcon(QtGui.QIcon(self.iconPath))
        self.hboxDate.addWidget(self.btnNextyear)
        self.btnNextyear.clicked.connect(self.toNextyear)

        #   日历本体
        self.mainCalendar = QtWidgets.QCalendarWidget()
        self.iconLeft = os.path.join(self.basePath, "Icon/left.png")
        self.iconright = os.path.join(self.basePath, "Icon/right.png")
        self.mainCalendar.setStyleSheet("QCalendarWidget QToolButton#qt_calendar_prevmonth{0}".format(self.iconLeft))
        self.mainCalendar.setVerticalHeaderFormat(QtWidgets.QCalendarWidget.VerticalHeaderFormat(0))
        self.mainCalendar.setNavigationBarVisible(False)
        self.calLayout.addWidget(self.mainCalendar)
        self.upDate()
        self.mainCalendar.currentPageChanged.connect(self.upDate)

        #   控制区域
        self.ctrlWidget = QtWidgets.QWidget()
        self.vbox.addWidget(self.ctrlWidget)
        self.ctrlLayout = QtWidgets.QHBoxLayout()
        self.ctrlLayout.setContentsMargins(0, 0, 0, 0)
        self.ctrlLayout.setSpacing(6)
        self.ctrlWidget.setLayout(self.ctrlLayout)

        self.ctrlLayout.addStretch(1)

        self.btnToday = QtWidgets.QPushButton("今日")
        self.iconPath = os.path.join(self.basePath, "Icon/today.png")
        self.btnToday.setIcon(QtGui.QIcon(self.iconPath))
        self.ctrlLayout.addWidget(self.btnToday)
        self.btnToday.clicked.connect(self.toToday)

        self.btnDetail = QtWidgets.QPushButton("详情")
        self.iconPath = os.path.join(self.basePath, "Icon/detail.png")
        self.btnDetail.setIcon(QtGui.QIcon(self.iconPath))
        self.ctrlLayout.addWidget(self.btnDetail)

    #   切换到上一年
    def toLastyear(self):
        self.mainCalendar.showPreviousYear()
    
    #   切换到上个月
    def toLast(self):
        self.mainCalendar.showPreviousMonth()
        
    #   切换到下个月
    def toNext(self):
        self.mainCalendar.showNextMonth()

    #   切换到下一年
    def toNextyear(self):
        self.mainCalendar.showNextYear()

    #   切换到今日
    def toToday(self):
        self.mainCalendar.setSelectedDate(datetime.datetime.today())

    #   刷新年月
    def upDate(self):
        thisYear = self.mainCalendar.yearShown()
        thisMonth = self.mainCalendar.monthShown()
        self.labDate.setText("{0}年{1}月".format(thisYear, thisMonth))